package com.xunge.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xunge.mapper.ExamRecordMapper;
import com.xunge.pojo.ExamRecord;
import com.xunge.pojo.ExamRecordQueryParam;
import com.xunge.pojo.PageResult;
import com.xunge.service.ExamRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ExamRecordServiceImpl implements ExamRecordService {

    @Autowired
    private ExamRecordMapper examRecordMapper;

    /**
     * 分页查询考试记录信息
     */
    @Override
    public PageResult<ExamRecord> page(ExamRecordQueryParam examRecordQueryParam) {
        // 设置分页参数
        PageHelper.startPage(examRecordQueryParam.getPage(), examRecordQueryParam.getPageSize());
        // 执行查询
        Page<ExamRecord> page = (Page<ExamRecord>) examRecordMapper.list(examRecordQueryParam);
        // 封装分页结果
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    /**
     * 新增考试记录
     */
    @Override
    public void save(ExamRecord examRecord) {
        examRecord.setCreateTime(LocalDateTime.now());
        examRecord.setUpdateTime(LocalDateTime.now());
        examRecordMapper.insert(examRecord);
    }

    /**
     * 根据ID查询考试记录
     */
    @Override
    public ExamRecord getById(Integer id) {
        return examRecordMapper.getById(id);
    }

    /**
     * 根据ID修改考试记录
     */
    @Override
    public void update(ExamRecord examRecord) {
        examRecord.setUpdateTime(LocalDateTime.now());
        examRecordMapper.update(examRecord);
    }

    /**
     * 根据ID删除考试记录
     */
    @Override
    public void deleteById(Integer id) {
        examRecordMapper.deleteById(id);
    }

    /**
     * 批量删除考试记录
     */
    @Override
    public void delete(List<Integer> ids) {
        examRecordMapper.deleteByIds(ids);
    }

    /**
     * 根据考试ID查询记录
     */
    @Override
    public List<ExamRecord> getByExamId(Integer examId) {
        return examRecordMapper.getByExamId(examId);
    }

    /**
     * 根据学生ID查询记录
     */
    @Override
    public List<ExamRecord> getByStudentId(Integer studentId) {
        return examRecordMapper.getByStudentId(studentId);
    }

    /**
     * 根据考试ID和学生ID查询记录
     */
    @Override
    public ExamRecord getByExamIdAndStudentId(Integer examId, Integer studentId) {
        return examRecordMapper.getByExamIdAndStudentId(examId, studentId);
    }
} 